﻿using project1113.Wrold;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using project1113.states;
using System.Windows.Threading;
using PRJ1109.Pages;
using project1113.FSM;

namespace project1113
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Fsm myFsm=new Fsm(new WaitState());
        
        public MainWindow()
        {
            InitializeComponent();
            DisplayHelper.Frame = mainFram;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Communication.Communication.CommunicateInit();

            DispatcherTimer _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromMicroseconds(100);
            _timer.Tick += _timer_Tick;
            _timer.Start();
        }
        private void _timer_Tick(object sender, EventArgs e)
        {
            if (myFsm != null)
            {
                WorldModel.CurrentWorld.WorldUpdate();                
                myFsm.RunOnece();
                //Debug.WriteLine(myFsm.CurrentState.Name);
            }
            

        }
    }

    public static class DisplayHelper
    {
        public static Frame Frame { get; set; }
        private static string currentFrameName;
   
        public static void LoadPage(string ? pageName)
        {
            if (currentFrameName == pageName)
            {
                return;
            }
            else
            {
                //释放前一个页面的内容
                Frame.Content = null;


                switch (pageName)
                {
                    case "TimePage":
                        WaitPage waitPage = new WaitPage();
                        if (waitPage!=null) 
                            Frame.Navigate(waitPage);
                        break;
                    case "ExecutePage":
                        Joy joy= new Joy();
                        if(joy!=null)
                             Frame.Navigate(new Joy());
                        break;
                    case "Emergence":
                        Scare scare= new Scare();
                        if (scare!=null) Frame.Navigate(new Scare());
                        break;
                    case "ChatPage":
                        Happy happy = new Happy();
                        if (happy!=null) Frame.Navigate(happy);
                        break;
                    case "Hello":
                        Hello hello = new Hello();
                        if (hello!=null) Frame.Navigate(hello);
                        break;
                    default:
                        break;
                }
                currentFrameName = pageName;
            }
        }
    }
}
